package com.qwiki.text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Corresponds to an internal link in wikipedia.
 * 
 * @author xgu
 * 
 * @since May 30, 2012
 */
public class ExternalLinkNode extends Node
{
  List<Node> children;

  @Override
  public String getText()
  {
    return "";// return getOriginalText();
  }

  @Override
  public List<Node> getChildren()
  {
    return Collections.EMPTY_LIST;
  }

  @Override
  public boolean parse(char[] buffer, int start)
  {
    children = new ArrayList<Node>();
    this.start = start;
    this.buffer = buffer;
    if (match(buffer, start, "["))
    {
      int i = start + 1;
      // find colon
      int colon = find(buffer, ":", i);
      if (colon - start > 10)
      {
        // not a proper external link if no : is found
        return parsed = false;
      }
      ExternalLinkNode node = new ExternalLinkNode();
      while (i < buffer.length)
      {
        // parse nested ExternalLinkNode
        if (node.parse(buffer, i))
        {
          i += node.getLength();
          node.parsed = false;
          continue;
        }
        // parse end of ExternalLinkNode
        if (match(buffer, i, "]"))
        {
          end = i + 1;
          parsed = true;
          return true;
        }
        i++;
      }
    }
    return false;
  }

}
